package com.itheima.tlias.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias.common.result.Result;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.pojo.entity.Emp;
import com.itheima.tlias.pojo.po.LoginParam;
import com.itheima.tlias.service.LoginService;
import com.itheima.tlias.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private EmpMapper empMapper;

    @Override
    public Result login(LoginParam param) {
        Emp emp = empMapper.selectEmpByUsername(param.getUsername());
        if (Objects.isNull(emp) || !Objects.equals(emp.getPassword(), param.getPassword())) {
            return Result.error("用户名或密码不正确");
        }
        String token = JwtUtil.generateToken(emp.getUsername(), emp.getPassword(), 30);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", emp.getId());
        jsonObject.put("username", emp.getUsername());
        jsonObject.put("name", emp.getName());
        jsonObject.put("token", token);
        return Result.success(jsonObject);
    }
}
